package com.ck.validate.config;

import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.Signature;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.stereotype.Component;

import javax.validation.ConstraintViolation;
import javax.validation.Path;
import javax.validation.Validator;
import java.lang.reflect.Method;
import java.util.Set;

@Slf4j
@Aspect
@Component
@AllArgsConstructor
public class ValidatorAspect {

    private Validator validator;

    //定义校验的包位置
    @Pointcut("execution(* com.ck..*.web.*.*(..))")
    public void pointcut() {
    }

    /**
     * 入参校验
     * @param joinPoint
     * @throws Throwable
     */
    @Around("pointcut()")
    public Object execute(ProceedingJoinPoint joinPoint) throws Throwable {
//        Result result = Result.newBuilder().build();
        String methodName = "";
        try{
            Signature sig = joinPoint.getSignature();
            MethodSignature msig = (MethodSignature) sig;
            Object target = joinPoint.getTarget();
            Method currentMethod = target.getClass().getMethod(msig.getName(), msig.getParameterTypes());
            methodName = currentMethod.getName();
        } catch (Exception e){
            log.error("无法获取方法名称" ,e);
        }
        Object[] args = joinPoint.getArgs();
        for (Object arg : args){
//            log.debug("→→→→→" + methodName + ">>>>Into parameter :" + JsonUtil.objectToJson(arg));
            if(arg != null){
                Set<ConstraintViolation<Object>> constraintViolations = validator.validate(arg);
                if(constraintViolations.size() > 0){
                    for (ConstraintViolation<Object> constraintViolation : constraintViolations) {
                        Path property = constraintViolation.getPropertyPath();
                        String name = property.iterator().next().getName();
//                        result = Result.newBuilder().fail().code(CommonReturnCode.PARAM_ERROR).message("[" + name + "]" + constraintViolation.getMessage()).build();
                        if (!constraintViolations.isEmpty()) {
                            throw new RuntimeException(constraintViolations.iterator().next().getMessage());
                        }

                        break;
                    }
//                    ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
//                    HttpServletResponse response = attributes.getResponse();
//                    String responseStr = JsonUtil.objectToJson(result);
//                    log.error(responseStr);
//                    log.debug("→→→→→" + methodName + ">>>>Return to the result :" + responseStr);
//                    HttpResponseUtil.setResponseJsonBody(response,responseStr);
                    return null;
                }
            }
        }
        Object object = joinPoint.proceed();
//        log.debug("→→→→→" + methodName + ">>>>Return to the result :" + object.;
        return object;
    }





}
